package com.zj.base.permission.checker;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.zj.base.permission.ConfigurationModules;
import com.zj.base.permission.interfaces.OnActionClickListener;
import com.zj.base.permission.interfaces.OnPermissionCheckListener;
import com.zj.base.permission.utils.JumpToSettingUtils;
import com.zj.base.utils.SPUtils;
import com.zj.base.utils.ToastUtils;

/**
 * Created by zhaojie on 2018/5/29.
 */

public class M_checker extends BasePermissionChecker {

    public M_checker(Activity activity) {
        super(activity);
    }

    @Override
    public void check() {
        //取出暂未被授权的
        for (ConfigurationModules module : getPermissions()) {
            if (listener != null)
                listener.startCheck(module);
            //将状态保存在map内，只通过系统授权询问一次，为false时表示用户已经拒绝；
            boolean hasPermission = (ContextCompat.checkSelfPermission(activity, module.permission) == PackageManager.PERMISSION_GRANTED);
            boolean isIgnore = isIgnore(module);
            if (!hasPermission && !isIgnore) {
                addFail(module);
            } else {
                addSuccess(module);
                if (listener != null)
                    listener.result(module, OnPermissionCheckListener.State.SUCCESS);
            }
        }
        if (isEnd()) {
            //请求的权限都已经授权
            if (listener != null)
                listener.onSuccess(getPermissions());
            SPUtils.put(activity, "isFirstIn", false);
        } else {
            //有没有完成授权的权限，需要进行申请授权；
            doAuthPermission();
        }
    }

    private void doAuthPermission() {
        //循环遍历授权内容，区分开用户操作情况；
        ConfigurationModules module = getFailPermission(0);
        boolean isNeverAsk = ActivityCompat.shouldShowRequestPermissionRationale(activity, module.permission);
        if (!isNeverAsk || !(PermissionCache.get(module.permission))) {
            if (!(boolean) SPUtils.get(activity, module.permission + "isFirst", false)) {
                //第一次进项目
                ActivityCompat.requestPermissions(activity, new String[]{module.permission}, PERMISSIONCODE);
                SPUtils.put(activity, module.permission + "isFirst", true);
            } else {
                //用户选择拒绝并且不再提示
                showDialog(module);
            }
        } else {
            //用户曾经拒绝，没有选择不再提示
            ActivityCompat.requestPermissions(activity, new String[]{module.permission}, PERMISSIONCODE);
        }
    }

    private void showDialog(final ConfigurationModules permission) {
        showAlert(true, permission, new OnActionClickListener() {
            @Override
            public void cancel(DialogInterface dialog) {
                if (onPermissionDialogCancel != null) {
                    onPermissionDialogCancel.onNegative(dialog, permission);
                } else {
                    if (permission.isForce) {
                        ToastUtils.show("必要权限获取失败，请重新授权");
                        if (activity != null)
                            activity.finish();
                        return;
                    } else {
                        addIgnore(permission);
                    }
                }
                if (listener != null)
                    listener.result(permission, permission.isForce ? OnPermissionCheckListener.State.FAIL : OnPermissionCheckListener.State.FAIL_NOT_FORCE);
                if (!isEnd()) check();
                else {
                    if (listener != null) listener.onSuccess(getPermissions());
                }
            }

            @Override
            public void fulfil(DialogInterface dialog) {
                JumpToSettingUtils.CheekAndJump(activity, PERMISSIONCODE);
            }
        });
    }
}
